

//
//  DFcomputerTableViewController.m
//  DFront
//
//  Created by lanou on 15/10/30.
//  Copyright © 2015年 xuzenghui. All rights reserved.
//

#import "DFcomputerTableViewController.h"
#import "DFcomputerModel.h"
#import "DFcomputerViewTableViewCell.h"
#import "PKRequestManager.h"
#import "DFClickAppleViewController.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"

@interface DFcomputerTableViewController ()

@property(nonatomic,strong)NSMutableArray *DataSourceArray;
@property(nonatomic, strong) DFcomputerModel *model;
@property(nonatomic, assign) int page;

@end

@implementation DFcomputerTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _DataSourceArray = [NSMutableArray array];
    
    [self.tableView registerClass:[DFcomputerViewTableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.rowHeight = 76;
    
    [self requestDataWithType:0];
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestDataWithType:0];
    }];
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self requestDataWithType:1];
    }];
    
}


- (void)requestDataWithType:(NSInteger)type
{
    if (type == 0) {
        _page = 1;
    }
    else
    {
        _page ++;
    }
    NSString *str = [NSString stringWithFormat:@"http://lib.wap.zol.com.cn/ipj/docList/?v=4.0&class_id=210&isReviewing=NO&last_time=2015-10-30%2015%3A55&page=%d&retina=1&vs=iph440",_page];
    [PKRequestManager requestWithType:GET urlString:str parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        if (type == 0) {
            [self.DataSourceArray removeAllObjects];
        }
        NSArray *listArray = dic[@"list"];
        for (NSDictionary *modelDic in listArray) {
            
            _model = [[DFcomputerModel alloc]init];
            
            [_model setValuesForKeysWithDictionary:modelDic];
            [_DataSourceArray addObject:_model];
            
            
            
        }
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        [self.tableView reloadData];
    } error:^(NSError *error) {
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        
    }];
    
    
    
    
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _DataSourceArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    DFcomputerViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"forIndexPath:indexPath];
    cell.ConfigureModel = _DataSourceArray[indexPath.row];
    DFcomputerModel *model =_DataSourceArray[indexPath.row];
    if (model.picArray.count != 0) {
        [cell.imgsrcImage sd_setImageWithURL:[NSURL URLWithString:model.picArray[0]]];
    }
   
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DFcomputerModel *model = _DataSourceArray[indexPath.row];
    DFClickAppleViewController *computerWeb = [[DFClickAppleViewController alloc] init];
    computerWeb.str = model.url;
    [self.navigationController pushViewController:computerWeb animated:YES];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
